<template>
  <el-form
    :model="form"
    :rules="rules2"
    ref="form"
    label-position="left"
    label-width="0px"
    class="demo-ruleForm login-container"
  >
    <h3 class="title">系统登录</h3>
    <el-form-item prop="account">
      <el-input type="text" v-model="form.account" auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input type="password" v-model="form.password" auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item>
      <div class="yzm">
        <el-input style="width:65%" v-model="form.code" placeholder="验证码"></el-input>
        <img ref="imgUrl" @click="changeUrl" :src="$url.yzm" alt height="30px" width="30%" />
      </div>
    </el-form-item>
    <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
    <el-form-item style="width:100%;">
      <el-button
        type="primary"
        style="width:100%;"
        @click.native.prevent="login"
        :loading="logining"
      >登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      logining: false,
      form: {
        account: "hy001",
        password: "111111",
        code: "6666"
      },
      rules2: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      },
      checked: true,
      n: 1
    };
  },

  methods: {
    changeUrl() {
      this.n++;
      this.$refs.imgUrl.src = this.$url.yzm + "?id=" + this.n;
    },
    water() {
      var waterList = [];
      var num = 0.0;
      for (var i = 0; i < 100; i++) {
        num = this.addFun(num, 0.001);
        waterList.push(num.toFixed(3));
      }
      this.$store.commit("getCut", JSON.stringify(waterList));
    },
    addFun(arg1, arg2) {
      var r1, r2, m;
      try {
        r1 = arg1.toString().split(".")[1].length;
      } catch (e) {
        r1 = 0;
      }
      try {
        r2 = arg2.toString().split(".")[1].length;
      } catch (e) {
        r2 = 0;
      }
      m = Math.pow(10, Math.max(r1, r2));
      return (arg1 * m + arg2 * m) / m;
    },
    login() {
      this.$api.post(this.$url.login, this.form, res => {
        console.log(res.data);
        if (res.data.code == 200) {
          this.$store.commit("getUserMsg", JSON.stringify(res.data.data));
          this.$message({
            message: "欢迎您回到代理平台",
            type: "success"
          });
          this.water();
          this.$router.push({ path: "/personalData" });
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login-container {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
}
.yzm {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>